﻿using System.Net;
using System.Net.Mail;
using System.Text;
using Sklep.Domain.Abstract;
using Sklep.Domain.Entities;

namespace Sklep.Domain.Concrete
{
    public class EmailOrderProcessor : IOrderProcessor
    {
        private readonly EmailSettings _emailSettings;


        public EmailOrderProcessor(EmailSettings emailSettings)
        {
            _emailSettings = emailSettings;
        }

        public void ProcessOrder(Cart cart, ShippingDetails shippingDetails)
        {
            using(var smtpClient = new SmtpClient())
            {
                smtpClient.EnableSsl = _emailSettings.UseSsl;
                smtpClient.Host = _emailSettings.ServerName;
                smtpClient.Port = _emailSettings.ServerPort;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(_emailSettings.UserName, _emailSettings.Password);

                if(_emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = _emailSettings.FileLocation;
                    smtpClient.EnableSsl = false;
                }

                StringBuilder body = new StringBuilder().AppendLine("Nowe zamówienie").AppendLine("----").
                    AppendLine("Produkty: ");

                foreach (var line in cart.Lines)
                {
                    var subtotal = line.Product.Price*line.Quantity;
                    body.AppendFormat("{0} x {1} (wartość: {2:c}", line.Quantity, line.Product.Name, subtotal);

                }

                body.AppendFormat("Wartość całkowita: {0:c}", cart.ComputeTotalValue()).
                    AppendLine("----").
                    AppendLine(shippingDetails.Name).
                    AppendLine(shippingDetails.Line1).
                    AppendLine(shippingDetails.Line2 ?? "").
                    AppendLine(shippingDetails.Line3 ?? "").
                    AppendLine(shippingDetails.City).
                    AppendLine(shippingDetails.State ?? "").
                    AppendLine(shippingDetails.Country).
                    AppendLine(shippingDetails.Zip).
                    AppendLine("---").
                    AppendFormat("Pakowanie Prezentu {0}", shippingDetails.GiftWrap ? "Tak" : "Nie");

                var mailMessage = new MailMessage(_emailSettings.MailFromAddress,
                    _emailSettings.MailToAddress,"New Order Submitted",body.ToString());

                if (_emailSettings.WriteAsFile)
                    mailMessage.BodyEncoding = Encoding.ASCII;

                smtpClient.Send(mailMessage);


            }
        }
    }


    public class EmailSettings
    {
        public string MailToAddress = "Zamowienia@przyklad.pl";
        public string MailFromAddress = "sklepSportowy@przyklad.pl";
        public bool UseSsl = true;
        public string UserName = "USerSmtp";
        public string Password = "PassSmtp";
        public string ServerName = "smtp.Przyklad.pl";
        public int ServerPort = 587;
        public bool WriteAsFile = true;
        public string FileLocation = @"d:\";
    }
}
